<template>
  <div>
    <n-modal
      v-model:show="showModal"
      :mask-closable="false"
      :show-icon="false"
      preset="dialog"
      transform-origin="center"
      :title="formValue.id > 0 ? '确认发货 #' + formValue.id : '添加物流信息'"
      :style="{
        width: dialogWidth,
      }"
    >
      <n-scrollbar style="max-height: 87vh" class="pr-5">
        <n-spin :show="loading" description="请稍候...">
          <n-form
            ref="formRef"
            :model="formValue"
            :rules="rules"
            :label-placement="settingStore.isMobile ? 'top' : 'left'"
            :label-width="100"
            class="py-4"
          >
            <n-grid cols="1 s:1 m:1 l:1 xl:1 2xl:1" responsive="screen">
              <n-gi span="1">
                <n-form-item label="收件人" path="recipient">
                  <n-input placeholder="请输入收件人" v-model:value="formValue.recipient" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="省市区" path="shippingProvinceId">
                  <!-- <n-input-number placeholder="请输入省市区id" v-model:value="formValue.shippingProvinceId" /> -->

                   <CitySelector v-model:value="formValue.shippingProvinceId" />
                   <!-- {{ formValue.shippingProvinceAddr }} -->
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="详细地址" path="shippingAddress">
                  <n-input placeholder="请输入详细地址" v-model:value="formValue.shippingAddress" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="联系方式" path="tel">
                  <n-input placeholder="请输入联系方式" v-model:value="formValue.tel" />
                </n-form-item>
              </n-gi>
              <!-- <n-gi span="1">
                <n-form-item label="状态 " path="status">
                  <n-select v-model:value="formValue.status" :options="dict.getOptionUnRef('ExpressStatusOptions')" />
                </n-form-item>
              </n-gi> -->
              <n-gi span="1">
                <n-form-item label="快递公司" path="expressCompany">
                  <n-select v-model:value="formValue.expressCompany" :options="dict.getOptionUnRef('ExpressOptions')" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="快递单号" path="expressNumber">
                  <n-input placeholder="请输入快递单号" v-model:value="formValue.expressNumber" />
                </n-form-item>
              </n-gi>
              <!-- <n-gi span="1">
                <n-form-item label="操作发货人" path="operatorId">
                  <n-input-number placeholder="请输入操作发货人" v-model:value="formValue.operatorId" />
                </n-form-item>
              </n-gi> -->
              <!-- <n-gi span="1">
                <n-form-item label="发货时间" path="dispatchTime">
                  <DatePicker v-model:formValue="formValue.dispatchTime" type="datetime" />
                </n-form-item>
              </n-gi> -->
            </n-grid>
          </n-form>
        </n-spin>
      </n-scrollbar>
      <template #action>
        <n-space>
          <n-button @click="closeForm">
            取消
          </n-button>
          <n-button type="info" :loading="formBtnLoading" @click="confirmForm">
            确定
          </n-button>
        </n-space>
      </template>
    </n-modal>
  </div>
</template>

<script lang="ts" setup>
  import { ref, computed } from 'vue';
  import { useDictStore } from '@/store/modules/dict';
  import { Edit, View, ToExpressed } from '@/api/expressInfo';
  import { GetCityLabel } from '@/api/apply/provinces';
  import { State, newState, rules } from './model';
  import DatePicker from '@/components/DatePicker/datePicker.vue';
  import { useProjectSettingStore } from '@/store/modules/projectSetting';
  import { useMessage } from 'naive-ui';
  import { adaModalWidth } from '@/utils/hotgo';

  const emit = defineEmits(['reloadTable']);
  const message = useMessage();
  const settingStore = useProjectSettingStore();
  const dict = useDictStore();
  const loading = ref(false);
  const showModal = ref(false);
  const formValue = ref<State>(newState(null));
  const formRef = ref<any>({});
  const formBtnLoading = ref(false);
  const dialogWidth = computed(() => {
    return adaModalWidth(840);
  });

  // 提交表单
  function confirmForm(e) {
    e.preventDefault();
    formRef.value.validate((errors) => {
      if (!errors) {
        formBtnLoading.value = true;
        ToExpressed(formValue.value)
          .then((_res) => {
            message.success('操作成功');
            closeForm();
            emit('reloadTable');
          })
          .finally(() => {
            formBtnLoading.value = false;
          });
      } else {
        message.error('请填写完整信息');
      }
    });
  }

  // 关闭表单
  function closeForm() {
    showModal.value = false;
    loading.value = false;
  }

  // 打开模态框
  function openModal(state: State) {
    showModal.value = true;

    // 新增
    if (!state || state.id < 1) {
      formValue.value = newState(state);

      return;
    }

    // 编辑
    loading.value = true;
    View({ id: state.id })
      .then((res) => {
        formValue.value = res;
        GetCityLabel({ oldId: res.shippingProvinceId, spilt: '-' }).then((resadd) => {
          console.log(resadd);

          formValue.value.shippingProvinceAddr = resadd;
        });
      })
      .finally(() => {
        loading.value = false;
      });
  }

  defineExpose({
    openModal,
  });
</script>

<style lang="less"></style>
